package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.CompetitionMapper;
import com.yang.zngcxyxkjs.mapper.TeacherMapper;
import com.yang.zngcxyxkjs.pojo.Competition;
import com.yang.zngcxyxkjs.pojo.LoginLog;
import com.yang.zngcxyxkjs.pojo.Student;
import com.yang.zngcxyxkjs.pojo.Teacher;
import com.yang.zngcxyxkjs.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin//跨域访问
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CompetitionMapper competitionMapper;

    @RequestMapping("/teacherFaceLogin")
    public String teacherFaceLogin(){
        return "teacher/teacherFaceLogin.html";
    }

    @RequestMapping("/teacherFaceIndex")
    public String teacherFaceIndex(HttpSession session,Model model){
        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);
        return "teacher/teacherIndex.html";
    }

    @RequestMapping("teacherlogin")
    public String toteacherLogin(){
        return "teacher/teacherLogin.html";
    }

    @RequestMapping("/teaLogin")
    public String teaLogin(String teaNo, String password, HttpSession session, Model model){
        Teacher tea = teacherMapper.teacherLogin(teaNo,password);
        if(tea!=null){
            LoginLog loginlog = new LoginLog();
            loginlog.setLoginAccount(teaNo);
            teacherMapper.insertTeacherLoginlog(loginlog);
            session.setAttribute("tea",tea);
            model.addAttribute("tea",tea);
            return "teacher/teacherIndex.html";
        }else{
            return "error.html";
        }
    }

    @PostMapping("/teacher/facerecognition")
    @ResponseBody
    public Teacher faceserach(@RequestParam String imgUrl,HttpSession session,Model model) throws Exception {
        Teacher teacher = teacherService.teaCompareFace(imgUrl);
        model.addAttribute("tea",teacher);
        LoginLog loginlog = new LoginLog();
        loginlog.setLoginAccount(teacher.getTeaNo());
        teacherMapper.insertTeacherLoginlog(loginlog);
        session.setAttribute("tea",teacher);
        return teacher;
    }

    @RequestMapping("/toTeaInfo")
    public String toTeaInfo(HttpSession session, Model model){
        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);
        return "teacher/teacherInfo.html";
    }

    @RequestMapping("/updateTeacherById")
    public String updateStudentById(Teacher teacher, Model model,HttpSession session){
        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);
        boolean i = teacherMapper.updateTeacher(teacher);
        model.addAttribute("i",i);
        if(i){
            model.addAttribute("tea",teacher);
            return "teacher/teacherInfo.html";
        }else{
            return "error.html";
        }
    }

    @RequestMapping("/tealogout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/teacherlogin";
    }

    @RequestMapping("/teagetAllCompetitionsByPages")
    public String getAllCompetitions(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session, Integer f){
        PageHelper.startPage(currentPageNum,5);
        List<Competition> list = competitionMapper.selectAllCompetitions();
        PageInfo<Competition> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);

        model.addAttribute("f",f);

        model.addAttribute("pageInfo",pageInfo);
        return "teacher/teacherAllCompetitions.html";
    }


}
